﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.ComponentModel;
using System.Reflection;

namespace SmartConference.Api.Filter
{
    public class DefaultValuesFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (schema == null)
            {
                return;
            }
            var obj = schema;
            foreach (var item in obj.Properties)
            {
                //if (item.Value.Type == "string" && item.Value.Default == null)
                //{
                //    item.Value.Default = new OpenApiString("");
                //}
                if (item.Key == "pageIndex")
                {
                    item.Value.Default = new OpenApiInteger(1);
                }
                else if (item.Key == "pageSize")
                {
                    item.Value.Default = new OpenApiInteger(10);
                }
                DefaultValueAttribute defaultValue = (context.ParameterInfo?.GetCustomAttribute<DefaultValueAttribute>());
                if (defaultValue != null)
                {
                    item.Value.Example = (IOpenApiAny)defaultValue.Value;
                }
            }
        }
    }
}
